//
//  ST_BTHUDController.swift
//  btvideo
//
//  Created by mac on 2020/4/2.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit


/**
 图文版本的HUD
 */
class ST_BTHUDController: UIViewController {

    
    static func toast_h_u_d(text:String,imageName:String = "icon_提示"){
        
        let thumbUpHud = ST_BTHUDController.init(noticeText: text,imageName: imageName)
        
        GlobalPresent(toController: thumbUpHud, animated: false)
        
        main_thread_execute(afterDelay: 2.5) {
            thumbUpHud.dismiss(animated: false, completion: nil)
        }
        
    }
    
    convenience init(noticeText:String,imageName:String = "icon_提示"){
        
        self.init(nibName: nil, bundle: nil)
        
        self.view.backgroundColor = .clear
        
        self.overCurrentContext = true
//        
        self.over_current_context_alpha = "0.2"
        
        let attrString = NSMutableAttributedString(string: noticeText)
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: UIColor(red: 1, green: 1, blue: 1, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        titleLabel.attributedText = attrString
        
        
        imageView.image = UIImage.init(named: imageName)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(138)
            make.center.equalToSuperview()
        }
        
        view.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.height.equalTo(60)
            make.top.equalTo(root_view).offset(24)
        }
        
        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(imageView.snp.bottom).offset(14)
        }
        
        
    }
    
    
//    MARK: - Properties
    fileprivate lazy var root_view:UIView = {
       let control = UIView()
        control.backgroundColor = UIColor.init(white: 0, alpha: 0.5)
        control.cornerRadius = 15
        return control
    }()
    
    
    fileprivate lazy var imageView:UIImageView = {
       let control = UIImageView()
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let label = UILabel()
        return label
    }()

}



class ST_BTHUDAlert: UIControl {
    
    convenience init(noticeText:String,imageName:String = "icon_提示"){
        
        self.init(frame: CGRect.zero)
        
        let attrString = NSMutableAttributedString(string: noticeText)
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),.foregroundColor: UIColor(red: 1, green: 1, blue: 1, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        titleLabel.attributedText = attrString
        imageView.image = UIImage.init(named: imageName)
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        self.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(138)
            make.center.equalToSuperview()
        }
        
        self.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.height.equalTo(60)
            make.top.equalTo(root_view).offset(24)
        }
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(imageView.snp.bottom).offset(14)
        }
        
        self.addTarget(self, action: #selector(close_handle), for: UIControl.Event.touchUpInside)
        
        main_thread_execute(afterDelay: 2.5) {
            self.close_handle()
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc fileprivate func close_handle(){
        
        self.removeFromSuperview()
        
    }
    
    @objc fileprivate func my_empty_handle(){
        
        print("占位事件")
    }
    
//    MARK: - Properties
    fileprivate lazy var root_view:UIControl = {
       let control = UIControl()
        control.backgroundColor = UIColor.init(white: 0, alpha: 0.5)
        control.addTarget(self, action: #selector(my_empty_handle), for: UIControl.Event.touchUpInside)
        control.cornerRadius = 15
        return control
    }()
    
    
    fileprivate lazy var imageView:UIImageView = {
       let control = UIImageView()
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let label = UILabel()
        return label
    }()
    
}
